package com.kk.base.controller.admin;

import com.kk.base.bean.CodeMsg;
import com.kk.base.bean.PageBean;
import com.kk.base.bean.Result;
import com.kk.base.entity.admin.Role;
import com.kk.base.entity.admin.UserRoleTypeEnum;
import com.kk.base.entity.home.BranchApplyFor;
import com.kk.base.entity.home.BranchApplyForStatusEnum;
import com.kk.base.service.admin.RoleService;
import com.kk.base.service.admin.UserService;
import com.kk.base.service.home.BranchApplyForService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @info : 前台网点申请网点申请控制层
 */

@RequestMapping("/admin/branchApplyFor")
@Controller
public class BranchApplyForController {


    @Autowired
    private BranchApplyForService branchApplyForService;
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    /**
     * 网点申请列表页面
     *
     * @param model
     * @param branchApplyFor
     * @param pageBean
     * @return
     */
    @RequestMapping(value = "/list")
    public String list(Model model, BranchApplyFor branchApplyFor, PageBean<BranchApplyFor> pageBean) {
        model.addAttribute("title", "网点申请列表");
        model.addAttribute("branchName", branchApplyFor.getBranchName());
        model.addAttribute("pageBean", branchApplyForService.findList(pageBean, branchApplyFor.getBranchName(), null));
        return "admin/branch_apply_for/list";
    }

    /**
     * 通过申请
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/agree", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> agree(@RequestParam(name = "id") Long id) {
        Role role = roleService.findTop1ByRoleType(UserRoleTypeEnum.BRANCHES);
        if (role == null) {
            return Result.error(CodeMsg.BRANCHAPPLYFOR_ROLE_NO_EXIST);
        }
        Integer integer = branchApplyForService.agreeBranchApplyFor(id, role);
        if (1 == integer) {
            return Result.error(CodeMsg.BRANCHAPPLYFOR_THROUGH_ERROR);
        }
        return Result.success(true);
    }

    /**
     * 驳回申请
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/rejected", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> rejected(@RequestParam(name = "id") Long id, @RequestParam(name = "rejectReason") String rejectReason) {
        BranchApplyFor branchApplyFor = branchApplyForService.find(id);
        branchApplyFor.setBranchApplyForStatus(BranchApplyForStatusEnum.NOTTHROUGH);
        if (StringUtils.isEmpty(rejectReason)) {
            return Result.error(CodeMsg.BRANCHAPPLYFOR_NOTTHROUGH_NULL);
        }
        if (rejectReason.length() >= 188) {
            return Result.error(CodeMsg.BRANCHAPPLYFOR_NOTTHROUGH_LENGTH);
        }
        branchApplyFor.setRejectReason(rejectReason);
        if (branchApplyForService.save(branchApplyFor) == null) {
            return Result.error(CodeMsg.BRANCHAPPLYFOR_NOTTHROUGH_ERROR);
        }
        return Result.success(true);
    }


}
